package uc108.com.jetpackdemo.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import uc108.com.jetpackdemo.R;
import uc108.com.jetpackdemo.bean.SelectInfo;
import uc108.com.jetpackdemo.viewmodel.SelectInfoViewModel;

public class LeftFragment extends Fragment {
    private SelectInfoViewModel selectInfoViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectInfoViewModel = ViewModelProviders.of(getActivity()).get(SelectInfoViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_left, container, false);
        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    private void init(View view) {

        view.findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectInfo info = new SelectInfo();
                info.index = 1;
                info.color = Color.RED;
                info.text = ((Button) view).getText().toString();
                info.textSize = 60;
                selectInfoViewModel.setData(info);
            }
        });
        view.findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectInfo info = new SelectInfo();
                info.index = 2;
                info.color = Color.GREEN;
                info.text = ((Button) view).getText().toString();
                info.textSize = 120;
                selectInfoViewModel.setData(info);
            }
        });
        view.findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectInfo info = new SelectInfo();
                info.index = 3;
                info.color = Color.BLACK;
                info.text = ((Button) view).getText().toString();
                info.textSize = 30;
                selectInfoViewModel.setData(info);
            }
        });
        view.findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SelectInfo info = new SelectInfo();
                info.index = 4;
                info.color = Color.BLUE;
                info.text = ((Button) view).getText().toString();
                info.textSize = 90;
                selectInfoViewModel.setData(info);
            }
        });
    }
}
